import { NestFactory } from '@nestjs/core';
import { RootModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { ResponseInterceptor } from './pipe/response';
import { AllExceptionFilter } from './pipe/exception';
import { RequestInterceptor } from './pipe/request';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { NoAuthGuard } from './common/any.strategy';

import * as serveStatic from 'serve-static';
import { join } from 'path';

async function bootstrap() {
	const app = await NestFactory.create(RootModule);

	// 允许cors
	app.enableCors();

	// 全局使用管道
	app.useGlobalPipes(new ValidationPipe({
		transform: true
	}));
	// 全局拦截器
	app.useGlobalInterceptors(new ResponseInterceptor());
	// 全局注册请求过滤器
	app.useGlobalInterceptors(new RequestInterceptor());
	// 全局注册错误的过滤器
	app.useGlobalFilters(new AllExceptionFilter());
	// 全局守卫
	app.useGlobalGuards(new NoAuthGuard())

	// 创建swagger文档
	const options = new DocumentBuilder()
    .setTitle("APP_NAME")
    .setDescription(`The APP_NAME API description`)
    .setVersion("1.0.0")
	.addBearerAuth()
    .build();
	const document = SwaggerModule.createDocument(app, options);
	SwaggerModule.setup('api-doc', app, document);

	// 静态目录
	app.use('/static', serveStatic(join(__dirname, '../static/')));

	await app.listen(3000);
}
bootstrap();
